package com.dhnsoft.代码随想录.字符串;

/**
 * ClassName: d06_28_找出字符串中第一个匹配项的下标
 * Package: com.dhnsoft.代码随想录.字符串
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/19 10:07
 * @Version 1.0
 */
public class d06_28_找出字符串中第一个匹配项的下标 {
    public static void main(String[] args) {
        System.out.println(strStr("mississippi","issip"));
    }

    public static int strStr(String haystack, String needle) {
        char[] haystackChars = haystack.toCharArray();
        char[] needleChars = needle.toCharArray();
        int len = haystackChars.length;
        int flag = -1;
        int j;
        int temp;
        for (int i=0;i<len;i++){
            j=0;
            temp = i;
            while (i<len&&haystackChars[i]==needleChars[j]){
                if(j==needleChars.length-1){
                    flag = i-j;
                    return flag;
                }
                i++;
                j++;
            }
            i = temp;
        }
        return flag;
    }
}
